package application.manager;

import application.entity.StageEntity;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
import lombok.Getter;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class FloatWindowManager {

    private static FloatWindowManager floatWindowManager = new FloatWindowManager();

    @Getter
    private volatile Map<Class<?>, StageEntity> stageEntities;

    private FloatWindowManager() {
        stageEntities = new HashMap<>();
    }

    public static FloatWindowManager getInstance() {
        if (floatWindowManager == null) {
            floatWindowManager = new FloatWindowManager();
        }
        return floatWindowManager;
    }

    public StageEntity register(String layout) throws IOException {
        layout = "popup_window/".concat(layout);
        URL resource = getClass().getClassLoader().getResource(layout);
        FXMLLoader fxmlLoader = new FXMLLoader(resource);
        Pane pane = fxmlLoader.load();
        Object controller = fxmlLoader.getController();
        StageEntity stageEntity = new StageEntity();
        stageEntity.setController(controller);
        stageEntity.setPane(pane);
        this.stageEntities.put(controller.getClass(), stageEntity);
        return stageEntity;
    }

    public Object getController(Class<?> cls) {
        return this.stageEntities.get(cls).getController();
    }

    public Pane getPane(Class<?> cls) {
        return this.stageEntities.get(cls).getPane();
    }
}
